package entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Base
 *
 * @author lenovo
 * @since 2023/9/4
 */
public class Base {

    /**
     * 公共查询
     * @param connection
     * @param preparedStatement
     * @param resultSet
     * @param sql
     * @param params
     * @return
     */
    public static ResultSet executequery(Connection connection, PreparedStatement preparedStatement,
                                         ResultSet resultSet, String sql, Object[] params){
        try {
            if (preparedStatement == null) {
                preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
            }
            for (int i = 0; params != null && i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }
            resultSet = preparedStatement.executeQuery();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return resultSet;

    }

    /**
     * 公共修改方法
     * @param connection
     * @param preparedStatement
     * @param sql
     * @param params
     * @return
     */
    public static int executeUpdate(Connection connection, PreparedStatement preparedStatement, String sql,
                                    Object[] params){
        int updateRows = 0;
        try {
            if (preparedStatement == null) {
                preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
            }
            for (int i = 0; params != null && i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }
             updateRows = preparedStatement.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return updateRows;

    }
}
